package com.eric;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.utils.collections.Maps;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class StartCheckWarTime {
  private static Logger _log = Logger.getLogger(StartCheckWarTime.class.getName());
  
  private static StartCheckWarTime _instance;
  
  private final Map<Integer, Data> _check;
  
  private StartCheckWarTime() {
    this._check = Maps.newHashMap();
    loadStartCheckWarTimeFromDatabase();
  }
  
  private void loadStartCheckWarTimeFromDatabase() {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM eric_startcheckwartime");
      rs = pstm.executeQuery();
      while (rs.next()) {
        Data data = new Data();
        int id = rs.getInt("castle_id");
        rs.getString("name");
        data._isActive = rs.getBoolean("isActive");
        this._check.put(new Integer(id), data);
      } 
      _log.config("StartCheckWarTime " + this._check.size());
    } catch (SQLException e) {
      e.printStackTrace();
      _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public static StartCheckWarTime getInstance() {
    if (_instance == null)
      _instance = new StartCheckWarTime(); 
    return _instance;
  }
  
  public boolean isActive(int castleId) {
    Data data = this._check.get(Integer.valueOf(castleId));
    return !(data != null && !((Data)this._check.get(Integer.valueOf(castleId)))._isActive);
  }
  
  private class Data {
    private Data() {}
    
    public boolean _isActive = true;
  }
}
